package top.drinyeit.nongmao.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;

/**
 * Created by wu on 2018/1/9.
 */

public class HomeClassfyAdapter extends RecyclerView.Adapter<HomeClassfyAdapter.ViewHolder> {
    private Context context;
    private List<CategoryGoodsDetailsBean> list;
    private Activity activity;

    public HomeClassfyAdapter(Activity activity, List<CategoryGoodsDetailsBean> list) {
        this.list = list;
        this.activity = activity;
    }


    @Override
    public HomeClassfyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_home_classfy, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(HomeClassfyAdapter.ViewHolder holder, int position) {
        holder.gv_home_classfy.setAdapter(new HomeClassfyGridAdapter(activity));
        setGridLayout(holder);
//        Glide.with(context).load(list.get(position).getCover_img_url())
//                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                .dontTransform()
//                .placeholder(R.drawable.ic_def)
//                .into(holder.iv_home_classfy_icon);

    }

    private void setGridLayout(HomeClassfyAdapter.ViewHolder holder) {
        int horizontalSpace = 15;
        int length = (activity.getWindowManager().getDefaultDisplay().getWidth() - horizontalSpace * 4) / 3;  //定义一个长度
        int size = 20;  //得到集合长度
        //获得屏幕分辨路
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        //                    Log.d(TAG, "handleMessage: "+density);
        int gridviewWidth = (int) (size * (length + horizontalSpace) );
        int itemWidth = (int) (length);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
        params.leftMargin = horizontalSpace;
        holder.gv_home_classfy.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        holder.gv_home_classfy.setColumnWidth(itemWidth); // 设置列表项宽
        holder.gv_home_classfy.setHorizontalSpacing(horizontalSpace); // 设置列表项水平间距
        holder.gv_home_classfy.setStretchMode(GridView.NO_STRETCH);
        holder.gv_home_classfy.setNumColumns(size); // 设置列数量=列表集合数
    }

    @Override
    public int getItemCount() {
        return list == null ? 10 : /*list.size()*/10;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_home_classfy_goods_icon;
        public GridView gv_home_classfy;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_home_classfy_goods_icon = itemView.findViewById(R.id.iv_home_classfy_goods_icon);
            gv_home_classfy = itemView.findViewById(R.id.gv_home_classfy);
        }


    }
}

